package com.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hospital.domain.BloodProductType;
import com.hospital.domain.HospitalInventory;
import com.hospital.service.BloodProductTypeService;
import com.hospital.service.HospitalInventoryService;
import com.hospital.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/hospitalInventory")
@CrossOrigin(origins = "*",maxAge = 3600)
public class HospitalInventoryController {

    @Autowired
    private HospitalInventoryService hospitalInventoryService;

    @Autowired
    private BloodProductTypeService bloodProductTypeService;

    @GetMapping("/getInventory")
    public R getInventory(){
        LambdaQueryWrapper<HospitalInventory> lqw = new LambdaQueryWrapper<>();
        lqw.select(HospitalInventory::getBloodType,HospitalInventory::getNum);
        List<Map<String,Object>> maps = hospitalInventoryService.listMaps(lqw);

        Map<String, BigDecimal> InventoryMap = new HashMap<>();

        for(Map<String,Object> map:maps){
            String bloodType = (String)map.get("blood_type");
            BigDecimal value =  (BigDecimal) map.get("num");
            InventoryMap.put(bloodType,InventoryMap.getOrDefault(bloodType, BigDecimal.ZERO).add(value));
        }

        List<Map<String,Object>> result = new ArrayList<>();
        for(Map.Entry<String, BigDecimal> entry : InventoryMap.entrySet()){
            Map<String,Object> m = new HashMap<>();
            m.put("bloodType",entry.getKey());
            m.put("num",entry.getValue());
            result.add(m);
        }
        return R.ok().put("data",result);
    }

    @GetMapping("getBloodProductType")
    public R getBloodProductType(){
        LambdaQueryWrapper<HospitalInventory> lqw = new LambdaQueryWrapper<>();
        lqw.select(HospitalInventory::getBloodProductTypeId, HospitalInventory::getNum);
        List<Map<String,Object>> maps = hospitalInventoryService.listMaps(lqw);

        Map<String, Object> mergedData = new HashMap<>();

        for (Map<String, Object> item : maps) {
            int bloodProductTypeId = (int) item.get("blood_product_type_id");
            BloodProductType bloodProductType = bloodProductTypeService.getById(bloodProductTypeId);
            String bloodProductTypeName = bloodProductType.getProductName();
            BigDecimal num = (BigDecimal) item.get("num");

            if (mergedData.containsKey(bloodProductTypeName)) {
                BigDecimal existingNum = (BigDecimal) mergedData.get(bloodProductTypeName);
                mergedData.put(bloodProductTypeName, existingNum.add(num));
            } else {
                mergedData.put(bloodProductTypeName, num);
            }
        }
        return R.ok().put("data",mergedData);
    }
}
